package com.xiaoxi.yygh.cmn.controller;

import com.xiaoxi.yygh.cmn.service.DictService;
import com.xiaoxi.yygh.common.result.Result;
import com.xiaoxi.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "数据接口字典")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {
    
    @Autowired
    private DictService dictService;
    
    @ApiOperation(value = "根据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }
    
    // 导出数据
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response){
        
        dictService.exportDictData(response);
        
    }
    
    // 导入数据
    @PostMapping("/importData")
    public Result importDict(@RequestParam("file") MultipartFile file){
        
        dictService.importDictData(file);
        
        return Result.ok();
    }
    
    // 根据 dictcode 与 value 查询
    @GetMapping("getName/{dictcode}/{value}")
    public String getName(@PathVariable String dictcode, @PathVariable String value){
        
        String dictName = dictService.getDictName(dictcode, value);
        
        return dictName;
    }
    
    // 根据value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        
        String dictName = dictService.getDictName("", value);
        
        return dictName;
    }

}
